﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Globalization;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Database db = new Database();
        db.OpenConnection();
        List<RBApplication> apps = new List<RBApplication>();
        using(Database.Command cmd = db.CreateCommand()) {
            cmd.CommandText = "SELECT id, displayname, defaultlanguage, languages FROM " + db.TablePrefix + "applications";
            DbDataReader reader = cmd.ExecuteReader();
            if(reader.HasRows) {
                while(reader.Read()) {
                    apps.Add(new RBApplication(reader["id"].ToString(), reader["displayname"].ToString(), reader["defaultlanguage"].ToString(), reader["languages"].ToString().Split(',')));
                }
                reader.Close();
                cmd.CommandText = "SELECT name FROM " + db.TablePrefix + "bundles WHERE application = @app ORDER BY name ASC";
                cmd.AddParameterWithValue("@app", string.Empty);
                StringBuilder buffer;
                List<string> bundles;
                foreach(RBApplication app in apps) {
                    cmd.Parameters["@app"].Value = app.ID;
                    buffer = new StringBuilder();
                    reader = cmd.ExecuteReader();
                    bundles = new List<string>();
                    while(reader.Read()) {
                        bundles.Add(reader.GetString(0));
                    }
                    app.Bundles = Utils.Implode(bundles, ", ", false);
                    reader.Close();
                }
                rptApplications.DataSource = apps;
                rptApplications.DataBind();
            } else {
                litApplications.Text = "<p>No applications have been added</p>";
            }
        }
    }

    private class RBApplication {
        private string id,
                       displayName,
                       defaultLanguage,
                       languages;

        public string ID { get { return id; } }
        public string DisplayName { get { return displayName; } }
        public string DefaultLanguage { get { return defaultLanguage; } }
        public string Languages { get { return languages; } }
        public string Bundles { get; set; }

        public RBApplication(string id, string displayName, string defaultLanguage, string[] languageCodes) {
            this.id = id;
            this.displayName = displayName;
            this.defaultLanguage = defaultLanguage;
            StringBuilder buffer = new StringBuilder();
            string[] languageNames = new string[languageCodes.Length];
            for(int i=0;i<languageCodes.Length;i++) {
                try {
                    languageNames[i] = CultureInfo.GetCultureInfo(languageCodes[i]).EnglishName;
                } catch {
                    languageNames[i] = languageCodes[i];
                }
            }
            Array.Sort(languageNames);
            string defaultName,
                   abbrev;
            try {
                defaultName = CultureInfo.GetCultureInfo(defaultLanguage).EnglishName;
            } catch {
                defaultName = defaultLanguage;
            }
            for(int i = 0; i < languageNames.Length; i++) {
                abbrev = languageNames[i];
                if(abbrev == defaultName) {
                    buffer.Append("<strong><em>").Append(abbrev).Append("</em></strong>");
                } else {
                    buffer.Append(abbrev);
                }
                if(i < languageCodes.Length - 1) {
                    buffer.Append(", ");
                }
            }
            this.languages = buffer.ToString();
        }
    }

    
}
